from typing import List

class TextSplitter:
    def split_with_format(self, text: str, min_segment_len: int = 150, overlap_len: int = 50) -> List[str]:
        """保留格式的语义分段
        
        Args:
            text: 输入文本
            min_segment_len: 最小分段长度
            overlap_len: 重叠长度
            
        Returns:
            List[str]: 分段后的文本列表
        """
        # 1. 按段落分割，保留原始格式
        paragraphs = self._split_into_paragraphs(text)
        
        # 2. 智能合并段落
        segments = []
        current_segment = ""
        
        for para in paragraphs:
            if len(current_segment) + len(para) < min_segment_len:
                current_segment += para
            else:
                if current_segment:
                    segments.append(current_segment)
                current_segment = para
                
                # 添加重叠部分
                if segments and overlap_len > 0:
                    current_segment = segments[-1][-overlap_len:] + current_segment
        
        if current_segment:
            segments.append(current_segment)
            
        return segments
    
    def _split_into_paragraphs(self, text: str) -> List[str]:
        """将文本分割为段落，保留格式
        
        Args:
            text: 输入文本
            
        Returns:
            List[str]: 段落列表
        """
        # 保留换行符和空格
        paragraphs = text.split('\n\n')
        # 过滤空段落，但保留格式
        return [p + '\n\n' if p.strip() else p for p in paragraphs if p] 